<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- symbol可以确保数据是唯一的 -->
      <!-- 用于老项目的迭代 -->
      <h2>依赖注入symbol</h2>
      <child></child>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const myInjectionKey = Symbol();
      const myInjectionKey2 = Symbol();

      const Child2 = {
        template: `
          <div>这是child2子组件 - {{ count1 }} - {{ count2 }}</div>
        `,
        inject: {
          count1: {
            from: myInjectionKey,
          },
          count2: {
            from: myInjectionKey2,
          },
        },
      };

      const Child = {
        data() {
          return {
            count: 200,
          };
        },
        template: `
          <div>这是child子组件</div>
          <child2></child2>
        `,
        components: {
          Child2,
        },
        provide() {
          return {
            [myInjectionKey2]: this.count,
          };
        },
      };

      Vue.createApp({
        data() {
          return {
            count: 100,
          };
        },
        components: {
          Child,
        },
        provide() {
          return {
            [myInjectionKey]: this.count,
          };
        },
      }).mount("#app");
    </script>
  </body>
</html>
